using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Principal;

using EAT.Common;

namespace EAT.ConfigurationUtility
{
    [Guid("AD48849A-7A32-46bb-83E7-CE6614A44935")]
    public class ConfigurationUtility : System.Web.UI.WebControls.WebParts.WebPart
    {
        private TextBox ManagementURL;
        private TextBox PwdExpire;
        private TextBox UACApprovers;
        private TextBox SCApprovers;
        private TextBox UserManagers;
        private TextBox eMailSource;
        private TextBox SMTPHost;
        private CheckBox SSPwdReset;
        private Button btnUpdateDB;
        private Label TextLabel;

        private SqlManager SqlM;
        private SPHelper SPH;

        public ConfigurationUtility()
        {
            this.ExportMode = WebPartExportMode.All;
            SPH = new SPHelper();
            SqlM = new SqlManager();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!SPH.IsAppAdmin())
            {
                TextLabel.Text = Resources.NoFullControl;
                return;
            }

            writer.Write("<table border=\"1\" width=\"100%\" class=\"ms-authoringcontrols\">");

            foreach (Control c in this.Controls)
            {
                writer.Write("<tr><td>");
                writer.Write(c.ID + " ");
                c.RenderControl(writer);
                writer.Write("</td></tr>");
            }

            writer.Write("</table>");
        }


        protected override void CreateChildControls()
        {
            if (SPH.IsAppAdmin())
            {
                ManagementURL = new TextBox();
                ManagementURL.ID = Resources.ManagementURLId;
                ManagementURL.ToolTip = Resources.ManagementURLTip;
                ManagementURL.Width = 500;
                ManagementURL.Text = HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "ManagementURL"));
                if (ManagementURL.Text.Length < 1)
                {
                    ManagementURL.Text = Resources.ManagementURLText;
                }
                this.Controls.Add(ManagementURL);

                PwdExpire = new TextBox();
                PwdExpire.ID = Resources.PWDExpireWindowId;
                PwdExpire.ToolTip = Resources.PWDExpireWindowTip;
                PwdExpire.Text = HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "PwdExpiration"));
                this.Controls.Add(PwdExpire);

                UACApprovers = new TextBox();
                UACApprovers.ID = Resources.UACApproversId;
                UACApprovers.ToolTip = Resources.UACApproversTip;
                UACApprovers.Text = HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "UACWorkflowApprovers"));
                UACApprovers.Width = 500;
                UACApprovers.MaxLength = 500;
                UACApprovers.EnableViewState = true;
                this.Controls.Add(UACApprovers);


                SCApprovers = new TextBox();
                SCApprovers.ID = Resources.SCApproversId;
                SCApprovers.ToolTip = Resources.SCApproversTip;
                SCApprovers.Width = 500;
                SCApprovers.Text = HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "SCWorkflowApprovers"));
                this.Controls.Add(SCApprovers);

                UserManagers = new TextBox();
                UserManagers.ID = Resources.UserManagersId;
                UserManagers.ToolTip = Resources.UserManagersTip;
                UserManagers.Width = 500;
                UserManagers.Text = HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "UserManagementGroup"));
                this.Controls.Add(UserManagers);

                SSPwdReset = new CheckBox();
                SSPwdReset.ID = Resources.SSPwdResetId;
                if (HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "SelfServicePwdResetEnabled")) == "true")
                {
                    SSPwdReset.Checked = true;
                }
                else
                {
                    SSPwdReset.Checked = false;
                }
                this.Controls.Add(SSPwdReset);

                eMailSource = new TextBox();
                eMailSource.ID = Resources.EmailSourceId;
                eMailSource.ToolTip = Resources.EmailSourceTip;
                eMailSource.Width = 500;
                eMailSource.Text = HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "eMailSource"));
                this.Controls.Add(eMailSource);

                SMTPHost = new TextBox();
                SMTPHost.ID = Resources.SMTPHostId;
                SMTPHost.ToolTip = Resources.SMTPHostTip;
                SMTPHost.Width = 500;
                SMTPHost.Text = HttpUtility.HtmlEncode(SqlM.GetEntry("Config", "SMTPHost"));
                this.Controls.Add(SMTPHost);

                btnUpdateDB = new Button();
                btnUpdateDB.Text = Resources.UpdateButtonText;
                btnUpdateDB.ToolTip = Resources.UpdateButtonTip;
                btnUpdateDB.Click += new EventHandler(UpdateDB_Click);
                this.Controls.Add(btnUpdateDB);
            }

            TextLabel = new Label();
            TextLabel.ToolTip = Resources.TextLabelTip;
            TextLabel.Text = "";
            this.Controls.Add(TextLabel);

        }


        void UpdateDB_Click(object sender, EventArgs e)
        {
            if (SPH.IsAppAdmin())
            {
                SqlM.SetEntry("Config", "ManagementURL", HttpUtility.HtmlEncode(ManagementURL.Text.Trim()));
                SqlM.SetEntry("Config", "PwdExpiration", HttpUtility.HtmlEncode(PwdExpire.Text.Trim()));
                SqlM.SetEntry("Config", "UACWorkflowApprovers", HttpUtility.HtmlEncode(UACApprovers.Text.Trim()));
                SqlM.SetEntry("Config", "SCWorkflowApprovers", HttpUtility.HtmlEncode(SCApprovers.Text.Trim()));
                SqlM.SetEntry("Config", "UserManagementGroup", HttpUtility.HtmlEncode(UserManagers.Text.Trim()));
                SqlM.SetEntry("Config", "eMailSource", HttpUtility.HtmlEncode(eMailSource.Text.Trim()));
                SqlM.SetEntry("Config", "SMTPHost", HttpUtility.HtmlEncode(SMTPHost.Text.Trim()));

                if (SSPwdReset.Checked)
                {
                    SqlM.SetEntry("Config", "SelfServicePwdResetEnabled", "true");
                }
                else
                {
                    SqlM.SetEntry("Config", "SelfServicePwdResetEnabled", "false");
                }   
                TextLabel.Text = Resources.DatabaseUpdated;
            }
        }
    }
}